package com.blog.zip.service;

import com.blog.zip.mapper.ArticleMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ArticleService {
    @Autowired(required = false)
    private ArticleMapper articleMapper;

    @CachePut(value = "article",key = "#article.get('id')")
    public Map addArticleToSolo(Map article){
        articleMapper.addArticleToSolo(article);
        Map<String, Object> result = articleMapper.getArticleByIdAdmin(article.get("id").toString());
        //log.info("存到数据库的值{}",result);
        return result;
    }

    @Cacheable(cacheNames = {"article"},key = "#id",unless="#result == null")
    public Map<String, Object> getArticleByIdV3(String id,Integer cmd) {

        Map<String, Object> result = new HashMap<>();

        if(cmd != null && cmd == 1){
            result  = articleMapper.getArticleByIdAdmin(id);
//            log.info("查询文章的id是{}",id);
//            log.info("查询文章的id是{}",article);
        }else{
            result = articleMapper.getArticleByIdV3(id);
        }
        return result;
    }

    @CachePut(value = "article",key = "#article.get('id')")
    public Map updateSoloArticle(Map article){
//        log.info("文章的id是{}",article.get("id"));
        articleMapper.updateSoloArticle(article);
        Map<String, Object> result = articleMapper.getArticleByIdAdmin((String) article.get("id"));
        return result;
    }

    // 分页查询 不定model层了
    public List<Map<String,Object>> getIndexList(Integer pageNo,Integer pageSize,Integer cmd){
        List<Map<String, Object>> list = new ArrayList<>();
        if (pageNo == null) pageNo = 1;
        if (pageSize == null) pageSize = 10;
        int offset = 0;
        offset = (pageNo - 1) * pageSize;

        // 获取首页分页的数据
        if(cmd == 1){
            // 获取置顶文章
            // 获取非置顶文章
            articleMapper.getArticleByIdV2(offset, pageSize, 0);
        }



        return list;
    }

}
